import {message} from "antd";

//计算百分比  Math.floor() 向下取整
export function totalPercentage(totalNum: number, freeNum: number): number {
    return Math.floor(((totalNum - freeNum) / totalNum) * 100);

}

//全屏浏览器
export function fullScreen() {
    try {
        const htmlEl = document.documentElement as any;
        htmlEl.requestFullscreen
            ? htmlEl.requestFullscreen()
            : htmlEl.webkitRequestFullScreen
            ? htmlEl.webkitRequestFullScreen()
            : htmlEl.mozRequestFullScreen
                ? htmlEl.mozRequestFullScreen()
                : htmlEl.msRequestFullscreen
                    ? htmlEl.msRequestFullscreen()
                    : message.warn('您所使用的浏览器不支持全屏');

    } catch (e) {
        message.warn('您所使用的浏览器不支持全屏');
    }
}

//退出全屏
export function exitFullScreen() {

    try {
        const doc = document as any
        doc.exitFullscreen
            ? doc.exitFullscreen()
            : doc.webkitCancelFullScreen
            ? doc.webkitCancelFullScreen()
            : doc.mozCancelFullScreen
                ? doc.mozCancelFullScreen()
                : doc.msExitFullscreen
                    ? doc.msExitFullscreen()
                    : message.warn('您所使用的浏览器不支持退出全屏, 请按ESC')
    } catch (e) {
        message.warn('您所使用的浏览器不支持退出全屏, 请按ESC')
    }
}

// 随机字符串
// Math.random() 函数返回一个浮点数,区间在[0,1)
const CODE = 'abcdefghjkmnpqrstuvwxyz13456789'

export function randomCode(num = 6) {
    let data = '';
    for (let i = 0; i < num; i++) {
        const random = Math.floor(Math.random() * CODE.length)
        data += CODE[random]
    }
    return data;
}

// 根据某个属性值从MenuList查找拥有该属性值的menuItem
export function getMenuItemInMenuListByProperty(menuList: any, key: any, value: any) {
    let stack: any[] = [];
    // debugger;
    console.log(menuList);
    stack = stack.concat(menuList);
    let res;
    while (stack.length) {
        let cur = stack.shift();
        if (cur.children && cur.children.length > 0) {
            stack = cur.children.concat(stack);
        }
        if (value === cur[key]) {
            res = cur;
        }
    }
    return res;
}

